package com.laiketui.app.services.dubbo;

import com.laiketui.app.api.RechargeService;
import com.laiketui.app.common.consts.AppConst;
import com.laiketui.common.mapper.FinanceConfigModelMapper;
import com.laiketui.common.mapper.PaymentConfigModelMapper;
import com.laiketui.domain.user.FinanceConfigModel;
import com.laiketui.domain.vo.MainVo;
import com.laiketui.root.annotation.DubboApiMethod;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.domain.lktconst.ErrorCode;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 充值
 *
 * @author Trick
 * @date 2020/11/4 16:20
 */
@DubboService(version = "1.0.0")
public class RechargeDubboServiceImpl implements RechargeService {

    private final Logger logger = LoggerFactory.getLogger(RechargeDubboServiceImpl.class);

    @DubboApiMethod(apiKey = "com.laike.app.rechargeIndex",
            login = false, module = AppConst.MODULE_NAME, version = "1.0.0")
    @Override
    public Map<String, Object> index(MainVo vo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            //获取最低充值金额
            FinanceConfigModel financeConfigModel = new FinanceConfigModel();
            financeConfigModel.setStore_id(vo.getStoreId());
            financeConfigModel = financeConfigModelMapper.selectOne(financeConfigModel);
            //获取支付方式
            Map<String,String> payConfigInfoMap = new HashMap<>(16);
            Map<String,Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id",vo.getStoreId());
            List<Map<String,Object>> resultPayConfigInfoMap = paymentConfigModelMapper.getPaymentConfigDynamic(parmaMap);
            for (Map<String, Object> map : resultPayConfigInfoMap){
                String key = map.get("class_name").toString();
                payConfigInfoMap.put(key,map.get("STATUS").toString());
            }

            resultMap.put("min_cz",financeConfigModel.getMin_cz());
            resultMap.put("payment",payConfigInfoMap);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("充值界面异常 " + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "index");
        }

        return resultMap;
    }

    @Autowired
    private PaymentConfigModelMapper paymentConfigModelMapper;

    @Autowired
    private FinanceConfigModelMapper financeConfigModelMapper;
}

